import {
  defineStore
} from "pinia";
//引入api
import {
  passwordLoginApi,
  messageLoginApi
} from "../apis/user";
import {
  nextTick,
  reactive,
  ref
} from "vue";
import {
  getExperienceApi
} from '@/apis/user'
import {
  getUnreadMessageApi,
  reduceUnreadMessageApi
} from '@/apis/unreadMessage'
import {
  getUserInfoApi,
  upgrade
}
from '@/apis/userInfo'

//定义并暴露一个store
//使用组合是因为store中有多个状态，所以使用组合的方式更加合适
export const useUserStore = defineStore("user", () => {
  //要用的数据直接定义，要用的方法直接定义，数据会被储存在localStorage中.直接将数据或函数暴露出去即可

  //定义一个空对象，用来存储用户信息
  const userInfo = ref({});

  const token = ref(null)

  //#region 密码登录代码
  const passwordLogin = async ({
    phone,
    password,
    captcha,
    captchaId
  }) => {

    const res = await passwordLoginApi({
      phone,
      password,
      captcha,
      captchaId
    })

    if (res.data) {
      //有数据就赋值
      userInfo.value = res.data
      token.value = res.data.token
    }
  }
  //#endregion


  //#region 短信登录代码
  const messageLogin = async ({
    phone,
    messageCaptcha
  }) => {

    const res = await messageLoginApi({
      phone,
      messageCaptcha
    })

    if (res.data) {
      //有数据就赋值
      userInfo.value = res.data
      token.value = res.data.token
    }


  }

  //#endregion


  // #region 获取用户信息代码
  /* 因为用户页也有可能是别人的页面。所以不能和userInfo用一个对象 */
  const info = ref({})

  const getUserInfo = async (id) => {
    if (id === undefined) {
      id = userInfo.value.id

      const res = await getUserInfoApi(id);

      userInfo.value = res.data
    } else {
      const res = await getUserInfoApi(id);

      info.value = res.data

    }

  }
  // #endregion




  // #region 退出登录代码
  const logout = () => {
    //清空用户信息
    // localStorage.removeItem("user");


    //发送请求去后端退出登录

    //清空vuex中的用户信息
    userInfo.value = {}

    //清空token
    token.value = null

    //清空未读信息
    unreadMessage.value = {}

    info.value = {}

    localStorage.removeItem("user");
  }


  // #endregion


  // #region 获得经验值
  // 经验值
  const experience = ref(0)

  // 获得经验值
  const getExperience = async () => {

    const res = await getExperienceApi(userInfo.value.id)

    experience.value = res.data

    isUpgrade()
  }


  // #endregion


  // #region 检测是否升级了
  const isUpgrade = async () => {

    /* 这里就等于升级了 */
    if (experience.value >= userInfo.value.needExperienceValue) {
      //发送请求，更新用户信息
      await upgrade(userInfo.value.id, experience.value)

      const res = await getUserInfoApi(userInfo.value.id);

      userInfo.value = res.data

      console.log(userInfo.value);


      await getExperience()
    }

  }

  // #endregion


  // #region 获取未读信息
  /* 定义一个空对象，存储所有的未读信息 */
  const unreadMessage = ref({});

  // 获取未读信息
  const getUnreadMessage = async () => {
    // 调用接口，获取未读信息
    const res = await getUnreadMessageApi(userInfo.value.id)

    // 将未读信息赋值给unreadMessage
    unreadMessage.value = res.data
  }

  /* 减少未读信息的数量 */
  const reduceUnreadMessage = async (type, number, ) => {
    // 调用接口，减少未读信息的数量
    await reduceUnreadMessageApi({
      type,
      number,
      userId: userInfo.value.id
    })
    // 重新获取未读信息
    await getUnreadMessage()
  }

  // #endregion


  return {
    userInfo,
    experience,
    unreadMessage,
    token,
    info,
    passwordLogin,
    messageLogin,
    getExperience,
    logout,
    getUnreadMessage,
    reduceUnreadMessage,
    getUserInfo
  }
}, {
  persist: {
    enabled: true,
    strategies: [{
      storage: localStorage,
      key: "user"
    }],
    paths: ['token', 'info']
  }
});